package io.renren.modules.work.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.work.entity.ScGoodsEntity;
import io.renren.modules.work.entity.ScStockEntity;
import io.renren.modules.work.service.ScGoodsService;
import io.renren.modules.work.service.ScStockService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 库存
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-03-24 23:31:29
 */
@RestController
@RequestMapping("work/scstock")
public class ScStockController {
    @Autowired
    private ScStockService scStockService;
    @Autowired
    private ScGoodsService scGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:scstock:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scStockService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{stockid}")
    @RequiresPermissions("work:scstock:info")
    public R info(@PathVariable("stockid") Integer stockid){

		ScStockEntity scStock = scStockService.getById(stockid);
		if(StringUtils.isNotBlank(scStock.getGoodsid())){
            ScGoodsEntity goods = scGoodsService.getById(scStock.getGoodsid());
            scStock.setGoodsname(goods.getGoodsname());
            scStock.setGoodsunit(goods.getGoodsunit());
            scStock.setGoodstype(goods.getGoodstype());

        }

        return R.ok().put("scStock", scStock);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:scstock:save")
    public R save(@RequestBody ScStockEntity scStock){
		scStockService.save(scStock);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("work:scstock:update")
    public R update(@RequestBody ScStockEntity scStock){
		scStockService.updateById(scStock);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("work:scstock:delete")
    public R delete(@RequestBody Integer[] stockids){
		scStockService.removeByIds(Arrays.asList(stockids));

        return R.ok();
    }

}
